package kr.hyunist.musicplayproject.Fragment;

import java.util.ArrayList;

import kr.hyunist.musicplayproject.R;
import kr.hyunist.musicplayproject.Activity.DetailActivity;
import kr.hyunist.musicplayproject.Adapter.ListAdapter;
import kr.hyunist.musicplayproject.DTO.MyMusic;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class AllListFragment extends Fragment {
	String TAG = "MEDIA";
	ArrayList<MyMusic> arList;
	ListAdapter allSongs;
	ListView allList;
	MediaPlayer mPlayer;
	boolean mMultiPane;
	int mLastIndex = 0;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.alllist, container, false);

		arList = new ArrayList<MyMusic>();
		allList = (ListView) root.findViewById(R.id.alllistfragmentlist);
		allSongs = new ListAdapter(getActivity(), arList);
		allList.setAdapter(allSongs);
		allList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				final int pos = position;
				Toast.makeText(getActivity(), "" + arList.get(pos).getData(), 0)
						.show();
				/*
				 * mPlayer = new MediaPlayer(); mPlayer.stop(); try {
				 * mPlayer.setDataSource(arList.get(pos).getData()); // mp3 파일의
				 * 경로를 인자로 mPlayer.prepare(); } catch(Exception e) { ; }
				 * 
				 * mPlayer.start();
				 */
				if (mMultiPane) {
					FragmentManager fm = getFragmentManager();
					DetailFragment df = (DetailFragment) fm
							.findFragmentById(R.id.alllistfragment);
				} else {
					Intent intent = new Intent(getActivity(),
							DetailActivity.class);
					intent.putExtra("title", arList.get(pos).getMusicTitle());
					intent.putExtra("artist", arList.get(pos).getArtist());
					intent.putExtra("album", arList.get(pos).getAlbumID());
					intent.putExtra("time", arList.get(pos).getPlayTime());
					startActivity(intent);
				}
			}

		});

		if (savedInstanceState != null) {
			mLastIndex = savedInstanceState.getInt("lastindex");
		}

		return root;
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("lastindex", mLastIndex);
	}
	
	

}
